package tree.treerecursion;

import tree.TreeNode;

/**
 * @Classname : SumOfLeftLeaves
 * @Description :
 * <a href="https://leetcode.cn/problems/sum-of-left-leaves/">404. 左叶子之和</a>
 * @Author : chentianyu
 * @Date 2023/1/20 09:55
 */


public class SumOfLeftLeaves {
    public int sumOfLeftLeaves(TreeNode root) {
        return sumOfLeftLeavesHelper(root, false);
    }

    // 先序遍历计算所有左叶子之和
    private int sumOfLeftLeavesHelper(TreeNode root, boolean isLeft) {
        if (root == null) return 0;

        if (isLeft && root.left == null && root.right == null) return root.val;
        return sumOfLeftLeavesHelper(root.left, true) + sumOfLeftLeavesHelper(root.right, false);
    }
}
